#pragma once
#include "QD3D/QD3DResourcePack.h"
#include "Octree.h"
#include "Primitives/CompactPrimitive.h"

struct FakeNode 
{
	FakeNode ()
	{
		memset (this, -1, sizeof (FakeNode));
	}
	int vertexTypes[8];
	int edgeIdxs[12];
	static const char *s_HLSL_STRUCT_STR;
};

class SurfaceExtractProcess
{
public:
	SurfaceExtractProcess ();
	~SurfaceExtractProcess ();

	void initialize (Octree *p_octree);
	void clean ();

	void process (Octree *p_octree, QD3DResourcePack *p_modelVerticesPack,
			QD3DResourcePack *p_modelTrianglesPack);

private:

	CONST_STRUCT_BEGIN(FaceSlotConstVars)
		int faceSlot;
	CONST_STRUCT_END

	Octree *_rp_octree;
	QD3DResourcePack *_rp_modelVerticesPack;
	QD3DResourcePack *_rp_modelTrianglesPack;

	ID3D11Buffer *_p_faceSlotDxConstBuf;

	QD3DResourcePack *_p_fakeNodesInfoPack;
	QD3DResourcePack *_p_triangleTablePack;
	QD3DResourcePack *_p_fakeNodesPack;
	QD3DResourcePack *_p_markPack;
	QD3DResourcePack *_p_ownerNodeIdxsPack;
	QD3DResourcePack *_p_compactedOwnerNodeIdxsPack;

	ID3D11ComputeShader *_p_calcNodeVertexValueCS;
	ID3D11ComputeShader *_p_genModelVerticesCS;
	ID3D11ComputeShader *_p_genTrianglesCS;
	ID3D11ComputeShader *_p_markFakeNodesCS;
	ID3D11ComputeShader *_p_genFakeNodesCS;
	ID3D11ComputeShader *_p_setFakeNodeVerticesAndEdgesCS;
	ID3D11ComputeShader *_p_genTrianglesInFakeNodesCS;
	ID3D11ComputeShader *_p_updateInfoInInitialCS;
	ID3D11ComputeShader *_p_updateInfoInGenFakeNodesCS;
	ID3D11ComputeShader *_p_updateInfoInGenTrianglesInFakeNodesCS;

	CompactPrimitive *_p_compactPrimitive;

	void _calcNodeVertexValue ();
	void _genModelVertices ();
	void _genTriangles ();
	void _markAndGenFakeNodes ();
	void _markFakeNodes ();
	void _genFakeNodes ();
	void _setFakeNodeVerticesAndEdges ();
	void _genTrianglesInFakeNodes ();
	void _updateInfoInInitial ();
	void _updateInfoInGenFakeNodes ();
	void _updateInfoInGenTrianglesInFakeNodes ();
};

